<?php
/*==========================================================================*/
/**  Model PHP class.
 *==========================================================================
 *
 *   LME - Starbugs - Typo 3.0 - Univis Module 
 *
 *   @par Copyright
 *   Copyright (c) FAU Erlangen-Nuernberg 2007 <BR>
 *   All Rights Reserved
 *
 *   @file model.php	
 *
 *
 *==========================================================================*/


class Model
{
    var $attributes = array();
    var $parentModelClass;
    var $parentModelKey;
    var $childModelKey;
    var $parentToTable = array();
    var $actualParentModel;
    var $department_id;

    function fitsIntoDOM($curModelClass, $parModelClass)
    {
	if (!$this->parentModelClass)
	{
	    DEBUG("It's a TOP-level model -> modelFits = true.\n");
	    return true;
	}
	if (is_string($this->parentModelClass))
	{
	    DEBUG("parentModelClass is a string\n");
	    if ($this->parentModelClass == $curModelClass)
	    {
		DEBUG("We are currently in a model class with the correct modelClass -> modelFits = true.\n");
		return true;
	    }
	    if ($this->parentModelClass == $parModelClass)
	    {
		DEBUG("We are currently in a model class with the correct parent->modelClass -> modelFits = true.\n");
		return true;
	    }
	}
	if (is_array($this->parentModelClass))
	{
	    DEBUG("parentModelClass is an array\n");
	    if (in_array($curModelClass, $this->parentModelClass))
	    {
		DEBUG("We are currently in a model class with the correct modelClass -> modelFits = true.\n");
		$this->actualParentModel = $curModelClass;
		return true;
	    }
	    if (in_array($parModelClass, $this->parentModelClass))
	    {
		DEBUG("We are currently in a model class with the correct parent->modelClass -> modelFits = true.\n");
		$this->actualParentModel = $parModelClass;
		return true;
	    }
	}
	return false;
    }

    function setAttribute($name, $value)
    {
	$name = Model::normalizeAttributeName($name);
	if (false !== $name)
	{
	    if (is_string($value))
	    {
		$value = utf8_decode($value);
	    }
	    $value = mysql_escape_string($value);
	    if (is_string($this->attributes[$name]))
	    {
		// mind the . (dot)!
		$this->attributes[$name] .= $value;
	    }
	    else
	    {
		$this->attributes[$name] = $value;
	    }
	    return true;
	}
	return false;
    }

    function getAttribute($name)
    {
	$name = Model::normalizeAttributeName($name);
	if (false !== $name)
	{
	    return $this->attributes[$name];
	}
	return null;
    }

    /* static */ function normalizeAttributeName($name)
    {
	$name = str_replace('-', '_', $name);
	$name = strtolower($name);
	if (isset($this->attributes[$name]))
	{
	    return $name;
	}
	return false;
    }

    function store()
    {
	$table = $this->table; // make a copy so we don't modify the object
	if (!empty($this->parentToTable)
		&& !empty($this->actualParentModel)
		&& !empty($this->parentToTable[$this->actualParentModel]))
	{
	    $table = $this->parentToTable[$this->actualParentModel];
	}
	$this->attributes['department_id'] = $this->department_id;
	foreach($this->attributes as $key => $value)
	{
		$this->attributes[$key] = trim($value);
	}
	$names =  implode("`, `", array_keys($this->attributes));
	$values = implode("', '", array_values($this->attributes));
	$q= "REPLACE INTO `{$table}` (`{$names}`) VALUES ('{$values}')";
//	print $q."\n";
	if (!mysql_query($q))
	{
	    trigger_error(mysql_error());
	    print 'Query was: '.$q."\n";
	}
    }
}
?>
